from os import error
from fastapi import APIRouter, HTTPException
from typing import List, Optional

from app.expert.models import Expert
from app.expert.schemas import ExpertSchema, ExpertCreate, ExpertUpdate, ExpertRelated
from app.expert.crud import expert


expert_router = APIRouter()

@expert_router.get("/{expert_id}", response_model=Optional[ExpertRelated])
async def get_expert(expert_id: int) -> Optional[Expert]:
    get_expert = await expert.get(id=expert_id)
    if not get_expert:
        raise HTTPException(
            status_code=404,
            detail="The expert with this id does not exist in the system",
        )
    return get_expert

@expert_router.get("/", response_model=List[ExpertSchema])
async def get_experts() -> List[Expert]:
    experts = await expert.get_multy()
    return experts

@expert_router.post("/create", response_model=ExpertSchema)
async def create_expert(expert_schema: ExpertCreate) -> Optional[Expert]:
    new_expert = await expert.create(schema=expert_schema)
    return new_expert

@expert_router.put("/update/{expert_id}", response_model=Optional[ExpertSchema])
async def update_expert(expert_id: int, schema_expert: ExpertUpdate) -> Optional[Expert]:
    updating_expert = await expert.update(id=expert_id, schema=schema_expert)
    if not updating_expert:
        raise HTTPException(
            status_code=404,
            detail="The expert with this id does not exist in the system",
        )
    return updating_expert

@expert_router.delete("/delete/{expert_id}", response_model=Optional[Expert])
async def delete_expert(expert_id: int) -> Optional[Expert]:
    deleting_expert = await expert.delete(id=expert_id)
    if not deleting_expert:
        raise HTTPException(
            status_code=404,
            detail="The expert with this id does not exist in the system",
        )
    return deleting_expert
